package org.belkodevelop.controller;

import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.belkodevelop.announcement.domain.Announcement;
import org.belkodevelop.announcement.dto.AnnouncementDto;
import org.belkodevelop.announcement.service.AnnouncementService;
import org.belkodevelop.controller.helper.AuthUtils;
import org.belkodevelop.form.NewRealtyForm;
import org.belkodevelop.realty.dto.RealtyStatus;
import org.belkodevelop.role.RoleConstants;
import org.belkodevelop.user.domain.User;
import org.dozer.DozerBeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/cabinet/")
public class CabinetController {
    
    private final Log log = LogFactory.getLog(getClass());

    @Autowired
    private AuthUtils authUtils;
    
    @Autowired
    private AnnouncementService announcementService;

    @Autowired
    private DozerBeanMapper mapper;
    
    @RequestMapping(value = {"", "/index.html"})
    public String index() {
        if (authUtils.getCurrentSessionUser().getRoles().contains(RoleConstants.ADMIN_ROLE)) {
            return "redirect:/admin/index.html";
        }
        return "cabinet";
    }
    
    @RequestMapping(method = RequestMethod.GET, value = "/api/deleteAnnouncement.json",  headers="Accept=*/*")
    @ResponseBody
    public RealtyStatus deleteAnnouncement(@RequestParam("id") Long id) {
        if (id != null) {
            boolean result = announcementService.removeAnnouncement(id, authUtils.getCurrentSessionUser().getId());
            return new RealtyStatus(!result);
        }
        return new RealtyStatus(true);
    }    
    
    @RequestMapping(method = RequestMethod.POST, value = "/api/addRealty.json",  headers="Accept=*/*")
    @ResponseBody
    public RealtyStatus addRealty(@RequestBody NewRealtyForm form) {
        try {
            Announcement announcement = form.getAnnouncement();
            announcement.setOwner(authUtils.getCurrentUser());
            announcementService.addAnnouncement(announcement);

            RealtyStatus realtyStatus = new RealtyStatus(false, null);
            realtyStatus.setAnnouncementId(announcement.getId());
            realtyStatus.setRealtyId(announcement.getRealty().getId());

            return realtyStatus;
        } catch (Exception ex) {
            log.error("add realty error", ex);
            return new RealtyStatus(true, ex.getMessage());
        }
    }
    
    @RequestMapping(method = RequestMethod.GET, value = "/api/getAnnouncements.json",  headers="Accept=*/*")
    @ResponseBody
    public List<AnnouncementDto> getAnnouncements() {
        User user = authUtils.getCurrentUser();
        if (user != null) {
            List<Announcement> announcementList = authUtils.getCurrentUser().getAnnouncements();
            List<AnnouncementDto> announcementDtoList = new ArrayList<AnnouncementDto>(announcementList.size());
            for (Announcement announcement: announcementList) {
                announcementDtoList.add(mapper.map(announcement, AnnouncementDto.class));
            }
            return announcementDtoList;
        } 
        return new ArrayList(0);
    }
    
}
